#!/bin/bash

# 移除现有的项目
sudo rm -rf /var/www/html

# 移除现有的Apache
yum remove httpd -y

# 安装Nginx和PHP-FPM
yum install -y nginx
yum install -y epel-release yum-utils
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php74
yum install -y php php-common php-cli php-fpm php-mysql php-gd php-xml php-mbstring php-json php-soap php-xmlrpc
sudo sed -i 's#listen = /run/php-fpm/www.sock#listen = 127.0.0.1:9000#' /etc/php-fpm.d/www.conf

# 启动并启用Nginx和PHP-FPM
systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm

# 创建PHP项目的目录并克隆代码库
sudo yum install -y git
mkdir -p /var/www/html
sudo git clone https://gitee.com/damon_liu_code/WeeklyReport.git /var/www/html
chown -R nginx:nginx /var/www/html/WeeklyReport
chmod -R 755 /var/www/html/WeeklyReport

# 创建Nginx配置文件
cat > /etc/nginx/nginx.conf << EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" \$status \$body_bytes_sent "\$http_referer" "\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        index index.php index.html index.htm;
        root         /var/www/html/WeeklyReport/public;

        location ~ \.php\$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
            include fastcgi_params;
        }

        location / {
            if (!-e \$request_filename){
                rewrite  ^(.*)\$  /index.php?s=\$1  last;
                break;
            }
        }

        location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md) {
            return 404;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)\$ {
            expires      30d;
            error_log /dev/null;
            access_log /dev/null;
        }

        location ~ .*\.(js|css)?\$ {
            expires      12h;
            error_log /dev/null;
            access_log /dev/null;
        }
    }
}
EOF

# 删除原有MySQL
sudo yum remove mysql57-community-release-el7 -y

# 安装并配置MySQL
sudo rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
sudo yum install -y https://repo.mysql.com/mysql57-community-release-el7.rpm
sudo yum install -y mysql-community-server
sudo systemctl start mysqld
sudo systemctl enable mysqld

# 获取MySQL初始临时密码
TEMP_PASS=$(sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}')

# 使用初始密码登录并修改root用户密码
mysql -u root -p"$TEMP_PASS" --connect-expired-password <<EOF
SET GLOBAL validate_password_policy=LOW;
SET GLOBAL validate_password_length=4;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
EOF

# 导入SQL文件到数据库
mysql -u root -proot <<EOF
CREATE DATABASE IF NOT EXISTS weekly_report CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF

mysql -u root -proot weekly_report < /var/www/html/WeeklyReport/weekly_report.sql


# 重启Nginx服务
systemctl restart nginx

# 确保防火墙设置允许80端口访问
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

# 获取服务器公网IP地址
IP=$(curl ifconfig.me)
echo "你的服务器公网IP地址是: $IP"

echo "脚本执行完毕，请通过该IP访问你的PHP项目。"
